Bug fixing nested_attributes update

jamesperet vor 11 Jahren
Ursprung
Commit
19006f411b

+ 5 - 0
app/assets/javascripts/missions.js.coffee

@@ -1,3 +1,8 @@
1 1
 # Place all the behaviors and hooks related to the matching controller here.
2 2
 # All this logic will automatically be available in application.js.
3 3
 # You can use CoffeeScript in this file: http://coffeescript.org/
4
+
5
+jQuery ->  $('form').on 'click', '.remove_fields', (event) ->    
6
+	$(this).prev('input[type=hidden]').val('1')    
7
+	$(this).closest('fieldset').hide()    
8
+	event.preventDefault()

+ 6 - 13
app/controllers/missions_controller.rb

@@ -58,20 +58,13 @@ class MissionsController < ApplicationController
58 58
   # PATCH/PUT /missions/1
59 59
   # PATCH/PUT /missions/1.json
60 60
   def update
61
-    params[:mission][:mission_agents_attributes].values.each do |a|
62
-      agent = MissionAgent.find(a[:id])
63
-      agent.description = a[:description]
64
-      if a[:mission_agent_steps_attributes] != nil
65
-        a[:mission_agent_steps_attributes].values.each do |s|
66
-          step = MissionAgentStep.find(s[:id])
67
-          step.description = s[:description]
68
-          step.save
69
-        end
70
-      end
71
-      agent.save
72
-    end
73 61
     respond_to do |format|
74 62
       if @mission.update(mission_params)
63
+        @mission.assign_attributes(params[:mission_agents_attributes])
64
+        @mission.assign_attributes(params[:mission_agent_steps_attributes])
65
+      
66
+        
67
+        
75 68
         format.html { redirect_to @mission, notice: 'Mission was successfully updated.' }
76 69
         format.json { head :no_content }
77 70
       else
@@ -138,6 +131,6 @@ class MissionsController < ApplicationController
138 131
 
139 132
     # Never trust parameters from the scary internet, only allow the white list through.
140 133
     def mission_params
141
-      params.require(:mission).permit(:title, :description, :status, :agent_search_start, :agent_search_end, :mission_agent_attributes, :mission_agent_step_attributes)
134
+      params.require(:mission).permit(:title, :description, :status, :agent_search_start, :agent_search_end, :mission_agents_attributes  => [:id, :mission_id, :description, :_destroy], :mission_agent_steps_attributes => [:id, :mission_agent_id, :description, :_destroy])
142 135
     end
143 136
 end

+ 2 - 1
app/models/mission.rb

@@ -1,5 +1,6 @@
1 1
 class Mission < ActiveRecord::Base
2 2
   belongs_to :owner, :class_name => "User"
3
-  has_many :mission_agents
3
+  has_many :mission_agents, :dependent => :destroy
4
+  has_many :mission_agent_steps, :through => :mission_agents
4 5
   accepts_nested_attributes_for :mission_agents, allow_destroy:true
5 6
 end

+ 2 - 2
app/models/mission_agent.rb

@@ -2,8 +2,8 @@ class MissionAgent < ActiveRecord::Base
2 2
   belongs_to :mission
3 3
   belongs_to :user
4 4
   
5
-  has_many :mission_agent_steps
6
-  has_many :mission_agent_invites
5
+  has_many :mission_agent_steps, :dependent => :destroy 
6
+  has_many :mission_agent_invites, :dependent => :destroy
7 7
   
8 8
   accepts_nested_attributes_for :mission_agent_steps, allow_destroy:true
9 9
   accepts_nested_attributes_for :mission_agent_invites

+ 2 - 2
app/views/missions/_mission_agent_form.html.erb

@@ -3,8 +3,8 @@
3 3
 	<% @step  = 1 %>
4 4
 	<%= f.label :description, "Description"%><br/>
5 5
 	<%= f.text_area :description%><br/>
6
-	<%= f.check_box :_destroy%>
7
-	<%= f.label :_destroy, "Remove Agent"%>
6
+	<%= f.hidden_field :_destroy%>
7
+	<%= link_to "remove", '#', class: "remove_fields"%>
8 8
 	<%= f.fields_for :mission_agent_steps do |step_builder| %>
9 9
 		<%= render 'mission_agent_step_form', f: step_builder %>
10 10
 	<%end%>

+ 2 - 2
app/views/missions/_mission_agent_step_form.html.erb

@@ -2,7 +2,7 @@
2 2
 	<h4>Step <%= @step %></h4>
3 3
 	<%= f.label :description, "Description"%>
4 4
 	<%= f.text_field :description %>
5
-	<%= f.hidden_field :_destroy %>
6
-	<%= f.label :_destroy, "Remove Step"%>
5
+	<%= f.hidden_field :_destroy%>
6
+	<%= link_to "remove", '#', class: "remove_fields"%>
7 7
 	<% @step = @step + 1 %>
8 8
 </fieldset>